//
//  PersonalChannelView.m
//  TestApplication
//
//  Created by Hanlingyu on 16/9/17.
//  Copyright © 2016年 ltq. All rights reserved.
//

#import "PersonalChannelView.h"
#import "PersonalViewModel.h"
#import "PersonChannelContent.h"
#import "ChannelPhotoCollectionViewCell.h"
#import "ChannelVoiceCollectionViewCell.h"
#import "UIImageView+WebCache.h"
#import "PersonChannelPosts.h"
#import "PersonChannelImg.h"
#import "TAMacro.h"
@interface PersonalChannelView ()
<
    UICollectionViewDelegate,
    UICollectionViewDataSource,
    UICollectionViewDelegateFlowLayout
>
@property (nonatomic, weak) UILabel *personalTitleLabel;
@property (nonatomic, strong) UICollectionView *personalPhotoCollectionView;
@property (nonatomic, weak) UILabel *moreLabel;

@end


@implementation PersonalChannelView


- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        
        [self setUpChildrenView];
        
    }
    return self;
}

- (void)setUpChildrenView {
    
    UILabel *personalTitleLabel = [[UILabel alloc] init];
//    personalTitleLabel.backgroundColor = [UIColor yellowColor];
    personalTitleLabel.font = TitleFont;
    [self addSubview:personalTitleLabel];
    _personalTitleLabel = personalTitleLabel;
    
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.minimumLineSpacing = 1.5;
    layout.minimumInteritemSpacing = 1.5;
     _personalPhotoCollectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
    _personalPhotoCollectionView.backgroundColor = [UIColor whiteColor];
    [self addSubview:_personalPhotoCollectionView];
    _personalPhotoCollectionView.dataSource = self;
    _personalPhotoCollectionView.delegate = self;
    
    [_personalPhotoCollectionView registerNib:[UINib nibWithNibName:@"ChannelPhotoCollectionViewCell" bundle:[NSBundle mainBundle]] forCellWithReuseIdentifier:@"ChannelPhotoCollectionViewCellReuse"];
    
    [_personalPhotoCollectionView registerNib:[UINib nibWithNibName:@"ChannelVoiceCollectionViewCell" bundle:[NSBundle mainBundle]] forCellWithReuseIdentifier:@"ChannelVoiceCollectionViewCellReuse"];
    
    
    
    UILabel *moreLabel = [[UILabel alloc] init];
//    moreLabel.backgroundColor = [UIColor redColor];
    moreLabel.text = @"查看更多 >";
    moreLabel.font = TextFont;
    moreLabel.textAlignment = 1;
    moreLabel.textColor = [UIColor lightGrayColor];
    [self addSubview:moreLabel];
    _moreLabel = moreLabel;
    
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    CGSize cellSize = CGSizeFromString(_personalViewModel.itemSizeArray[indexPath.row]);

    return cellSize;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {

    return  _personalViewModel.itemSizeArray.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    if ([_personalViewModel.personalContent.posts[indexPath.row].postType isEqualToString:@"Voice"]) {
        
        ChannelVoiceCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"ChannelVoiceCollectionViewCellReuse" forIndexPath:indexPath];
        cell.voice = _personalViewModel.personalContent.posts[indexPath.row].voice;
        return cell;
    }
    
    ChannelPhotoCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"ChannelPhotoCollectionViewCellReuse" forIndexPath:indexPath];
    [cell.photoImage sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://qn.zheli.io/%@", _personalViewModel.personalContent.posts[indexPath.row].img[0].f]]];
    return cell;
}


- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    NSLog(@"第%ld个cell", indexPath.row);
    // block传值
    PersonChannelPosts *post = _personalViewModel.personalContent.posts[indexPath.row];
    self.postNextPage(post);
    
    
}



- (void)setPersonalViewModel:(PersonalViewModel *)personalViewModel {
    _personalViewModel = personalViewModel;
    // Frame
    [self setUpFrame];
    // Data
    [self setUpData];
    
    [_personalPhotoCollectionView reloadData];
}

- (void)setUpFrame {
    _personalTitleLabel.frame = _personalViewModel.titleLabelFrame;
    _personalPhotoCollectionView.frame = _personalViewModel.contentCollectionViewFrame;
    _moreLabel.frame = _personalViewModel.moreLabelFrame;
}

- (void)setUpData {
    _personalTitleLabel.text = [NSString stringWithFormat:@"%@ · %ld", _personalViewModel.personalContent.content, _personalViewModel.personalContent.postCount];

}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
